package com.zjw.concurrency.juc.quenu;

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockQueueExample {

    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue(3);

        Interview interview = new Interview(queue);
        Consumer consumer = new Consumer(queue);
        new Thread(interview).start();
        new Thread(consumer).start();
    }


}

class Interview implements Runnable{
    ArrayBlockingQueue<String> queue;

    public Interview(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {

        System.out.println("面试官已经全部就绪");
        for (int i = 0; i < 10; i++) {
            String interviewer = "Candidate" + i;
            try {
                queue.put(interviewer);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            queue.put("over");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer implements Runnable{

    ArrayBlockingQueue<String> queue;

    public Consumer(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            String msg;
            while(!(msg = queue.take()).equals("over")){
                System.out.println(msg + "到了");
            }
            System.out.println("面试结束，回家等通知");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
